<?php

namespace system\core;

class execute extends pluginmanager
{

	private $callingObject;

	function __call($name, $args)
	{
//		echo $name."\n";
//		echo "Given Args: ";
//		print_r($args);

		if( self::$methods ) foreach( self::$methods as $objectName => $registeredMethods )
			{
				if( isset($registeredMethods[$name]) )
				{
					$executeOn = $registeredMethods[$name];
//					$reflect = new \ReflectionObject($executeOn);
//					$reflectParameter = $reflect->getMethod($name)->getParameters();
					$executeOn->setCaller($this->getCallingObject());
					$executeOn->initialize();

					call_user_func_array(array($executeOn, $name), $args);

					return $executeOn->getCaller();
				}
			}
		throw new Exception("There is no registered method like $name", 10);

//		print_r(self::$methods);
//		print_r(self::$registered);
	}

	function setCallingObject($object)
	{
		$this->callingObject = $object;
	}

	function getCallingObject()
	{
		return $this->callingObject;
	}

}

class Exception extends \Exception
{

	function __construct($message="", $code=0, $previous=NULL)
	{
		switch ($code)
		{
			case 10:
				$this->notRegisteredFunction();
				break;
			default:
				break;
		}
	}

	function notRegisteredFunction()
	{
		$error = array_pop($this->getTrace());
		trigger_error("Undefined function {$error["function"]}() in {$error["file"]} on line {$error["line"]} raised",E_USER_ERROR);
	}

}

?>
